/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.unifei.VRaptor.infra;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.ComponentFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Ramon Moraes
 */
@Component
@ApplicationScoped
public class CriadorDeEntityManager implements ComponentFactory<EntityManager>{
    
    private EntityManagerFactory emf;
    private EntityManager em;
    
    @PostConstruct
    public void abre(){
        this.emf = Persistence.createEntityManagerFactory("default");
        this.em = emf.createEntityManager();
    }
    
    public EntityManager getInstance(){
        return this.em;
    }
    
    @PreDestroy
    public void fecha(){
        this.em.close();
        this.emf.close();
    }
}
